<!DOCTYPE HTML PUBLIC "-//ORA//DTD CD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>[Chapter 2] 2.4 StringTokenizer</TITLE>
<META NAME="author" CONTENT="Mark Grand and Jonathan Knudsen">
<META NAME="date" CONTENT="Fri Aug  8 16:07:57 1997">
<META NAME="form" CONTENT="html">
<META NAME="metadata" CONTENT="dublincore.0.1">
<META NAME="objecttype" CONTENT="book part">
<META NAME="otheragent" CONTENT="gmat dbtohtml">
<META NAME="publisher" CONTENT="O'Reilly &amp; Associates, Inc.">
<META NAME="source" CONTENT="SGML">
<META NAME="subject" CONTENT="Java">
<META NAME="title" CONTENT="Java Fundamental Classes Reference">
<META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript">
</HEAD>
<body vlink="#551a8b" alink="#ff0000" text="#000000" bgcolor="#FFFFFF" link="#0000ee">

<DIV CLASS=htmlnav>
<H1><a href='index.htm'><IMG SRC="gifs/smbanner.gif"
     ALT="Java Fundamental Classes Reference" border=0></a></H1>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch02_03.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><B><FONT FACE="ARIEL,HELVETICA,HELV,SANSERIF" SIZE="-1">Chapter 2<br>Strings and Related Classes</FONT></B></TD>
<td width=172 align=right valign=top><A HREF="ch03_01.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
</table>

&nbsp;
<hr align=left width=515>
</DIV>
<DIV CLASS=sect1>
<h2 CLASS=sect1><A CLASS="TITLE" NAME="JFC-CH-2-SECT-4">2.4 StringTokenizer</A></h2>

<P CLASS=para>
<A NAME="CH02.TOKEN1"></A><A NAME="CH02.TOKEN2"></A>The <tt CLASS=literal>java.util.StringTokenizer</tt> 
class provides support for parsing a string into a sequence of words, or 
<I CLASS=emphasis>tokens</I>, that are 
separated by some set of delimiter characters. Here is an example of how 
to use the <tt CLASS=literal>StringTokenizer</tt> 
class: 

<DIV CLASS=screen>
<P>
<PRE>
StringTokenizer s = new StringTokenizer("This is it");
while (s.hasMoreTokens())
    System.out.println(s.nextToken());
</PRE>
</DIV>

<P CLASS=para>
This example begins by creating a <tt CLASS=literal>StringTokenizer</tt> 
object to pick tokens out of the specified string. The example uses a <tt CLASS=literal>StringTokenizer</tt> 
constructor that does not specify what delimiters to use, so the new <tt CLASS=literal>StringTokenizer</tt> 
object uses the default delimiters: space, 
tab (<tt CLASS=literal>'\t'</tt>), 
carriage return (<tt CLASS=literal>'\r'</tt>), 
and newline (<tt CLASS=literal>'\n'</tt>). 

<P CLASS=para>
The <tt CLASS=literal>while</tt> loop does the actual 
work of getting the tokens from the <tt CLASS=literal>StringTokenizer</tt> 
object. The <tt CLASS=literal>hasMoreTokens()</tt> 
method returns <tt CLASS=literal>true</tt> while there 
are still more tokens to be fetched from the <tt CLASS=literal>StringTokenizer</tt> 
object, while <tt CLASS=literal>nextToken()</tt> returns 
the next token. Here is the output from the example: 

<DIV CLASS=screen>
<P>
<PRE>
This
is
it
</PRE>
</DIV>

<P CLASS=para>
You can also use a <tt CLASS=literal>StringTokenizer</tt> 
to extract tokens from a string that uses delimiters other than whitespace. 
For example, suppose that you need to extract tokens that are separated 
by commas, such as from a string that looks like this: 

<DIV CLASS=screen>
<P>
<PRE>
abc,def,123,789
</PRE>
</DIV>

<P CLASS=para>
In this case, you use a <tt CLASS=literal>StringTokenizer</tt> 
constructor that takes a parameter that specifies the characters to be 
treated as delimiters. For example: 

<DIV CLASS=screen>
<P>
<PRE>
StringTokenizer s = new StringTokenizer(commaString, ",");
</PRE>
</DIV>

<P CLASS=para>
The second argument to this constructor specifies the delimiter characters, 
so in this case, the only delimiter character is the comma character. 

</DIV>


<DIV CLASS=htmlnav>

<P>
<HR align=left width=515>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch02_03.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><a href="index.htm"><img src='gifs/txthome.gif' border=0 alt='Home'></a></td>
<td width=172 align=right valign=top><A HREF="ch03_01.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
<tr>
<td width=172 align=left valign=top>String Concatenation</td>
<td width=171 align=center valign=top><a href="index/idx_0.htm"><img src='gifs/index.gif' alt='Book Index' border=0></a></td>
<td width=172 align=right valign=top>Threads</td>
</tr>
</table>
<hr align=left width=515>

<IMG SRC="gifs/smnavbar.gif" USEMAP="#map" BORDER=0> 
<MAP NAME="map"> 
<AREA SHAPE=RECT COORDS="0,0,108,15" HREF="../javanut/index.htm"
alt="Java in a Nutshell"> 
<AREA SHAPE=RECT COORDS="109,0,200,15" HREF="../langref/index.htm" 
alt="Java Language Reference"> 
<AREA SHAPE=RECT COORDS="203,0,290,15" HREF="../awt/index.htm" 
alt="Java AWT"> 
<AREA SHAPE=RECT COORDS="291,0,419,15" HREF="../fclass/index.htm" 
alt="Java Fundamental Classes"> 
<AREA SHAPE=RECT COORDS="421,0,514,15" HREF="../exp/index.htm" 
alt="Exploring Java"> 
</MAP>
</DIV>

</BODY>
</HTML>
